<?php
namespace User\Controller;
use Think\Controller;
/**
 * 分享报表
 */
class ShareController extends CkController {
    
    //直接分享报表
    public function index(){
       $user=$this->userinfo;
       $M= M("jifeiip_log");
       $jinri_time = strtotime(date('Y-m-d',time()));
       $zuori_time= $jinri_time-(3600*24);
       $where_jinri = array("uid"=>$user['id'],"time"=>array('EGT',$jinri_time));
       $where_zuori= array("uid"=>$user['id'],"time"=>array('EGT',$zuori_time),'time'=>array('LT',$jinri_time));
       $data['jinri']= $M->field('SUM(price) as price')->where($where_jinri)->select();
       $data['zuori']= $M->field('SUM(price) as price')->where($where_zuori)->select();
       $data['jinri']= $data['jinri'][0];
       $data['zuori']= $data['zuori'][0];
       $data['user_info'] = $user;
       $data['title']="分享报告";
       $this->assign($data);
       $this->display();
    }

    
    //24小时收益报表
    public function h24()
    { 
       $user=$this->userinfo;
       $time = time()-(3600*24);
       $page =(int) I("get.page");
       $count = M("jifeiip_log")->field("count(distinct(news_id)) as count")->where("uid = ".$user['id']." and time >=".$time)->select();
       $count =  $count['0']['count'];
       $page_size=6;
       $page_total_num = ceil($count/$page_size);
       $page_start = $page * $page_size;
       $data=array(
            "total_num" => $count,
            "page_size" => $page_size,
            "page_total_num" => $page_total_num,
        );
       $data['list']=D("Jifei")->Relation("news")->field("news_id,count(*) AS count")->where("uid = ".$user['id']." and time >=".$time)->limit($page_start,$page_size)->order("count desc")->group("news_id")->select();
       //P($data); 
       $this->ajaxReturn($data);
    }


    //下级收益报表
    public function fans()
    {  
       $user=$this->userinfo;
       if(C("JIAKUAN_LOG_FANS")){
       $jinri_time = strtotime(date('Y-m-d',time()));
       $zuori_time= $jinri_time-(3600*24);
       $where_jinri = array("uid"=>$user['id'],"time"=>array('EGT',$jinri_time));

       $where_zuori= array("uid"=>$user['id'],"time"=>array('EGT',$zuori_time),'time'=>array('LT',$jinri_time));

       $data['jinri']=M("jiakuan_xiaji_log")->field('SUM(price) as price')->where($where_jinri)->select();
       $data['zuori']=M("jiakuan_xiaji_log")->field('SUM(price) as price')->where($where_zuori)->select();
       $data['jinri']= $data['jinri'][0]['price'] ? $data['jinri'][0]['price'] : '0.0000';
       $data['zuori']= $data['zuori'][0]['price'] ? $data['zuori'][0]['price'] : '0.0000';
       }
       $data['fans']=$this->user_yi($user['id']);
       $data['xiaji']= $user['xiaji'];
       $data['user_info'] = $user;
       $data['title']="粉丝收益报告";
       $this->assign($data);
       $this->display();
    }

	//邀请收益报表
    public function invited()
    {  
       $user=$this->userinfo;
       if(C("JIAKUAN_LOG_FANS")){
       $jinri_time = strtotime(date('Y-m-d',time()));
       $zuori_time= $jinri_time-(3600*24);
       $where_jinri = array("uid"=>$user['id'],"time"=>array('EGT',$jinri_time),"type"=>3);

       $where_zuori= array("uid"=>$user['id'],"time"=>array('EGT',$zuori_time),'time'=>array('LT',$jinri_time),"type"=>3);

       $data['jinri']=M("jiakuan_xiaji_log")->field('SUM(price) as price')->where($where_jinri)->select();
       $data['zuori']=M("jiakuan_xiaji_log")->field('SUM(price) as price')->where($where_zuori)->select();
       $data['jinri']= $data['jinri'][0]['price'] ? $data['jinri'][0]['price'] : '0.0000';
       $data['zuori']= $data['zuori'][0]['price'] ? $data['zuori'][0]['price'] : '0.0000';
       }
       $data['fans']=$this->user_yi($user['id']);
       $data['xiaji']= $user['xiaji'];
       $data['user_info'] = $user;
       $data['title']="邀请收益报告";
       $this->assign($data);
       $this->display();
    }


    //递归获取本会员的是有下级粉丝的数量
    private  function user_fans_num2($uid,$arr)
    {
       $array= array();
       $user=M("user")->field("id,user_pid")->where("user_pid=".$uid)->select();
       if($user){
           foreach ($user as $key => $v) {

         $array=array_merge($arr,self::user_fans_num($v['id'],$arr));
         }
       }
    
       
       P($array);
    }



    //递归获取上级会员
    private function user_fans_num($user_pid,$data=null)
    {  
       $arr=array(); 

       if($user_pid!==0){
          $user=M("user")->field("id,user_pid")->where("user_pid=".$user_pid)->find();
          if($user['user_pid']!=="0"){
            $arr[] = $user['user_pid'];
            $arr=array_merge($arr,self::user_fans_num($user['user_pid'],$arr));
          }
       }
      return $arr;
    }
    
    
    //计算5级下线数量，写的有点垃圾
    private function user_yi($uid)
    {
        $user1 = M("user")->field("id,user_pid")->where("user_pid=".$uid)->select();
        $xiaji[1]=count($user1);

        foreach ($user1 as $key=>$v) {
           $user1[$key]["x"]=self::user_er($v['id']);
           $xiaji[2]+= count($user1[$key]["x"]);
        }
      foreach ($user1 as $key => $v) {
          if(is_array($v['x']))
          {
             foreach ($v['x'] as $key2 => $v2) {
                $user1[$key]["x"][$key2]['x']=self::user_er($v2['id']);
                $xiaji[3]+= count($user1[$key]["x"][$key2]['x']);
                if(is_array($user1[$key]["x"][$key2]['x']))
                {
                    foreach ($user1[$key]["x"][$key2]['x'] as $key3 => $v3) {
                         $user1[$key]["x"][$key2]['x'][$key3]['x']=self::user_er($v3['id']);
                         $xiaji[4]+= count($user1[$key]["x"][$key2]['x'][$key3]['x']);
                         

                            if(is_array($user1[$key]["x"][$key2]['x'][$key3]['x']))
                            {
                            foreach ($user1[$key]["x"][$key2]['x'][$key3]['x'] as $key4 => $v4) {
                            $user1[$key]["x"][$key2]['x'][$key3]['x'][$key4]['x']=self::user_er($v4['id']);
                            $xiaji[5]+= count($user1[$key]["x"][$key2]['x'][$key3]['x'][$key4]['x']);

                            }
                       }

                    }
                }
           }
        }
      }
      
    return $xiaji;
    }
    
    private function user_er($uid)
    {  
       $a=M("user")->field("id,user_pid")->where("user_pid =".$uid)->select();
       return $a;
    }

   
    //分销收益
    function shop()
    {  
       $user=$this->userinfo;
       $data['dai']= M('shop_order')->field("SUM(suk_ticheng) as suk_ticheng,count(*) AS count")->where(array('_string'=>"type=1 OR type=2 OR type=9",'uid'=>$user['id']))->find();
       $data['yi']= M('shop_order')->field("SUM(suk_ticheng) as suk_ticheng,count(*) AS count")->where(array("type"=>3,'uid'=>$user['id']))->find();
       $data['shou']= M('shop_order')->field("SUM(suk_ticheng) as suk_ticheng,count(*) AS count")->where(array("type"=>4,'uid'=>$user['id']))->find();
       $data['ju']= M('shop_order')->field("SUM(suk_ticheng) as suk_ticheng,count(*) AS count")->where(array("type"=>5,'uid'=>$user['id']))->find();
       $data['wu']= M('shop_order')->field("SUM(suk_ticheng) as suk_ticheng,count(*) AS count")->where(array("type"=>7,'uid'=>$user['id']))->find();
       $data['shop_list']=M('shop_item')->field('id,title')->select();
       if(I('get.item_id')){
          $where['yz_shop_order.item_id']=I('get.item_id');
       }
       if(I('get.type')){
          $where['yz_shop_order.type']=I('get.type');
       }
       $where['uid']=$user['id']; 
       $data['order_list']=M('shop_order')->where($where)->order("yz_shop_order.id desc")->join('yz_shop_item ON yz_shop_order.item_id = yz_shop_item.id')->select();
 
       $this->assign($data);
       $this->display();
    }
    

    //我的徒弟
    public function tudi()
    {
        $user=$this->userinfo;
        $data['tudi']= M('user')->where(array('user_pid'=>$user['id']))->select();
        if($data['tudi']){
        $tudi_uid= $this->xiaji_uid($data['tudi']);
        $data['tuzi']=M('user')->where(array('user_pid'=>array('in',$tudi_uid)))->select();
         if($data['tuzi']){
          $tuzi_uid= $this->xiaji_uid($data['tuzi']);
          $data['tushun']=M('user')->where(array('user_pid'=>array('in',$tuzi_uid)))->select();
          if($data['tushun']){
            $tuzi_4_uid= $this->xiaji_uid($data['tushun']);
            $data['tu4']=M('user')->where(array('user_pid'=>array('in',$tuzi_4_uid)))->select(); 
            if($data['tu4']){
               $tuzi_5_uid= $this->xiaji_uid($data['tu4']);
               $data['tu5']=M('user')->where(array('user_pid'=>array('in',$tuzi_5_uid)))->select();   
              }
            }
          }
        }
        $this->assign($data);
        $this->display();
    }

    
    //获取上级uid
    private function xiaji_uid($user)
    {  
       $a='';
       foreach ($user as $v) {
         if(!empty($a)){
             $fuhao=","; 
         }
         $a=$a.$fuhao.$v['id'];
       }
       return $a;
    }

   
    //订单详细
    public function order()
    {   
        $user=$this->userinfo;
        $id=I('get.id');
        $where['uid']=$user['id']; 
        $where['order_id']=$id;
        $data['order']=M('shop_order')->where($where)->find();
        if(empty($order)){
            $this->error("订单不存在");
        }else{
            $this->assign($data);
            $this->display();
        }
    }


    

}